﻿#include "Snake.h"



void SetPos(int y,int x)
{
	//获得设备句柄
	HANDLE handle = NULL;
	handle = GetStdHandle(STD_OUTPUT_HANDLE);
	//设置光标的位置
	COORD pos = { y,x };
	SetConsoleCursorPosition(handle, pos);
}
void WelcomeToGame()
{
	//打印欢迎信息
	SetPos(38,12);
	printf("欢迎来到贪吃蛇小游戏");
	SetPos(40, 20);
	system("pause");
	system("cls");

	//打印功能介绍信息
	SetPos(35, 9);
	
	printf("规则如下: ");
	SetPos(35, 10);
	printf("1.用W,S,A,D 来控制蛇的上下左右移动");
	SetPos(35, 11);
	printf("2.H是加速，J是减速");
	SetPos(35, 12);
	printf("3.加速可以得到更高的分数");
	SetPos(35, 13);
	printf("4.开始游戏后，右侧可以看到得分情况");
	SetPos(40, 20);
	system("pause");
	system("cls");

}
void DrawMap()
{
	//上
	SetPos(0, 0);
	for (int i = 0; i < 57; i+=2)
		wprintf(L"%c", WALL);


	//下
	SetPos(0, 26);
	for (int i=0;i<57;i+=2)
		wprintf(L"%c", WALL);

	//左
	SetPos(0,1);
	for (int i =1;i<=25; i++)
		wprintf(L"%c\n", WALL);


	//右
	
	for (int i = 1; i <= 25; i++)
	{
		SetPos(56, i);
		wprintf(L"%c\n", WALL);
	}
	SetPos(0, 27);
	system("pause");

}


void InitSnake(PSnake ps)
{
	//开始创建蛇身节点
	PSnakeNode cur = NULL;
	for (int i = 0; i < 5; i++)
	{
		cur = (PSnakeNode)malloc(sizeof(SnakeNode));
		if (cur == NULL)
		{
			perror("InitSnake :malloc failed!");
			return;
		}
		cur->x = POS_X + 2 * i;
		cur->y = POS_Y;
		cur->next = NULL;

		//头插
		if (ps->PSnake == NULL)
			ps->PSnake = cur;
		else
		{
			cur->next = ps->PSnake;
			ps ->PSnake = cur;
		}

	}
	

	//打印蛇身
	cur = ps->PSnake;
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%c",BODY);
		cur = cur->next;
	}


	//修改蛇的内部属性
	ps->dir = RIGHT;
	ps->FoodWeight = 10;
	ps->PFood = NULL;
	ps->Score = 0;
	ps->SleepTime = 200;
	ps->status = OK;
}

void CreateFood(PSnake ps)
{
	//1.随机出现
	//2.墙体以内
	//3.不能再蛇的身体上

	int x = 0, y = 0;
	//循环创建食物坐标，保证食物符合上诉的三个要求
again:
	srand((unsigned int)time(NULL));
	do
	{
		
		x = rand() % 53 + 2;
		y = rand() % 24 + 1;
	} while (x % 2 != 0);

	//将坐标和蛇的身体作比较
	PSnakeNode cur = ps->PSnake;
	while (cur)
	{
		if (x == cur->x && y == cur->y)
			goto again;
		cur = cur->next;
	}

	//创建食物
	PSnakeNode pf = (PSnakeNode)malloc(sizeof(SnakeNode));
	if (pf == NULL)
	{
		perror("CreateFood :malloc failed!");
		return;
	}
	pf->x = x;
	pf->y = y;
	ps->PFood = pf;

	//打印食物
	SetPos(pf->x, pf->y);
	wprintf(L"%c", FOOD);
}

void GameStart(PSnake ps)
{
	//设置控制台的信息，窗口大小，窗口名
	system("mode con cols=100 lines=30");
	system("title 贪吃蛇");

	//设置光标信息--隐藏光标
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO cursor_info = { 0 };
	GetConsoleCursorInfo(handle, &cursor_info);
	cursor_info.bVisible = false;
	SetConsoleCursorInfo(handle, &cursor_info);
	
	//打印欢迎信息
	WelcomeToGame();

	//绘制地图
	DrawMap();

	//初始化蛇
	InitSnake(ps);

	//创建食物
	CreateFood(ps);
	/*PSnakeNode pf = (PSnakeNode)malloc(sizeof(SnakeNode));
	if(pf==)
	pf->x = 40;
	pf->y = 5;
	pf->next = NULL;
	ps->PFood = pf;
	SetPos(pf->x, pf->y);
	wprintf(L"%c", FOOD);*/
	
}


void PrintAssist()
{
	SetPos(65, 13);
	printf("1.不能穿墙，不能咬到自己");
	SetPos(65,14);
	printf("4.用W,S,A,D 来控制蛇的上下左右移动");
	SetPos(65, 15);
	printf("3. H是加速，J是减速");
	SetPos(65, 16);
	printf("4.加速可以得到更高的分数");
	SetPos(65,25);
	printf("Made By 糊涂涂");
}


void Pause()
{
	while (1)
	{
		Sleep(300);
		if (KEY_PRESS(VK_SPACE))
		{
			break;
		}
	}
}
bool NextIsFood(PSnake ps, PSnakeNode PNextNode)
{
	if (ps->PFood->x == PNextNode->x && ps->PFood->y == PNextNode->y)
		return true;
	return false;
}

void EatFood(PSnake ps, PSnakeNode PNextNode)
{
	//先连接节点
	PNextNode->next = ps->PSnake;
	ps->PSnake = PNextNode;

	//打印蛇身
	PSnakeNode cur = ps->PSnake;
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%c", BODY);
		cur = cur->next;
	}


	//改变内部属性
	ps->Score += ps->FoodWeight;
	free(ps->PFood);
	CreateFood(ps);

	
}

void NotEatFood(PSnake ps, PSnakeNode PNextNode)
{
	//先连接节点
	PNextNode->next = ps->PSnake;
	ps->PSnake = PNextNode;

	PSnakeNode cur = ps->PSnake;
	


	while (cur->next->next)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%c", BODY);
		cur = cur->next;
	}
	//最后⼀个位置打印空格，然后释放节点
	SetPos(cur->next->x, cur->next->y);
	printf("  ");
	free(cur->next);
	cur->next = NULL;
}
void KillByWall(PSnake ps)
{
	if (ps->PSnake->x == 0 ||
		ps->PSnake->y == 26 ||
		ps->PSnake->y == 0 ||
		ps->PSnake->x == 56
		)
	{
		ps->status = KILL_BYWALL;
	}
}
void KillBySelf(PSnake ps)
{
	PSnakeNode cur = ps->PSnake->next;
	while (cur)
	{
		if (cur->x == ps->PSnake->x && cur->y == ps->PSnake->y)
		{
			ps->status = KILL_BYSELF;
			return;
		}
		cur = cur->next;
	}
}

void SnakeMove(PSnake ps)
{
	//判断蛇的方向
	//这里创建一个PNextNode节点用于判断下一个位置，以及是否和食物重合
	PSnakeNode PNextNode = (PSnakeNode)malloc(sizeof(SnakeNode));
	if (PNextNode == NULL)
	{
		perror("SnakeMove:malloc failed!");
		return;
	}
	PNextNode->next = NULL;
	switch (ps->dir)
	{
		case UP:
			PNextNode->x = ps->PSnake->x;
			PNextNode->y = ps->PSnake->y-1;
			break;
		case DOWN:
			PNextNode->x = ps->PSnake->x;
			PNextNode->y = ps->PSnake->y + 1;
			break;
		case LEFT:
			PNextNode->x = ps->PSnake->x - 2;
			PNextNode->y = ps->PSnake->y;
			break;
		case RIGHT:
			PNextNode->x = ps->PSnake->x + 2;
			PNextNode->y = ps->PSnake->y;
			break;
	}

	//判断是否和食物重合
	if (NextIsFood(ps, PNextNode))
		EatFood(ps,PNextNode);
	else
		NotEatFood(ps,PNextNode);

	//检查是否撞墙
	KillByWall(ps);
	//检查是否撞自己
	KillBySelf(ps);

}
void GameRun(PSnake ps)
{
	//打印帮助信息
	PrintAssist();
	do
	{
		//当前的分数情况
		SetPos(65,10);
		printf("Total Points:%04d", ps->Score);
		SetPos(65,11);
		printf("Food Score:%02d", ps->FoodWeight);
		 
		//检测按键

		if (KEY_PRESS('W') && ps->dir != DOWN)
		{
			ps->dir = UP;
		}
		else if (KEY_PRESS('S') && ps->dir != UP)
		{
			ps->dir = DOWN;
		}
		else if (KEY_PRESS('A') && ps->dir != RIGHT)
		{
			ps->dir = LEFT;
		}
		else if (KEY_PRESS('D') && ps->dir != LEFT)
		{
			ps->dir = RIGHT;
		}
		else if (KEY_PRESS(VK_SPACE))
		{
			Pause();
		}
		else if (KEY_PRESS(VK_ESCAPE))
		{
			ps->status = ESC;
			break;
		}
		else if (KEY_PRESS(VK_SPACE))
		{
			Pause();
		}
		else if (KEY_PRESS('H'))
		{
			if (ps->SleepTime >= 80)
			{
				ps->SleepTime -= 30;
				ps->FoodWeight += 2;
			}
		}
		else if (KEY_PRESS('J'))
		{
			if (ps->FoodWeight >2)
			{
				ps->SleepTime += 30;
				ps->FoodWeight -= 2;
			}
		}

		//睡眠一下
		Sleep(ps->SleepTime);
		//走一步
		SnakeMove(ps);

	} while(ps->status ==OK);
	

}

void GameEnd(PSnake ps)
{
	SetPos(23, 12);
	switch (ps->status)
	{
	case ESC:
		printf("Exit successfully!");
		break;
	case KILL_BYWALL:
		printf("You're failed");
		break;
	case KILL_BYSELF:
		printf("You're failed");
		break;
	}

	//释放资源
	PSnakeNode cur = ps->PSnake;
	while (cur)
	{
		PSnakeNode del = cur;
		cur = cur->next;
		free(del);
	}
	free(ps->PFood);
	ps->PFood=ps->PSnake = NULL;
	ps = NULL;
}